<?php

namespace App\Http\Controllers;

use App\Models\ActivityProjectVideo;
use App\Models\Maker;
use App\Models\Project;
use App\Models\Topic;
use App\Models\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\TopicRequest;
class TopicController extends CommonController
{
    /**
     * 获取系统设置列表
     */
    public function getIndex()
    {
        $lists = Topic::orderBy('id', 'asc')->paginate(10);

        return view('topic.index')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = Topic::orderBy('recommend', 'desc')->orderBy('id', 'asc');

        if (isset($data['subject']) && $data['subject'] != '') {
            $sql->where('subject', 'like', '%' . trim($data['subject']) . '%');
        }

        if (isset($data['description']) && $data['description'] != '') {
            $sql->where('description', 'like', '%' . trim($data['description']) . '%');
        }

        if (isset($data['type']) && $data['type'] != '') {
            $sql->where('type', $data['type']);
        }

        $lists = $sql->paginate(10);

        return view('topic.search')->with("lists", $lists)->with("subject", trim($data['subject']))
            ->with("type", $data['type'])->with("description", $data['description'])->with("actions", $this->actions);
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        return view('topic.create');
    }

    public function postTargetSearch(Request $request)
    {
        $data = $request->input();
        $type = $data['type'];
        if($type==''){
            return AjaxCallbackMessage('请输入专题类型', false);
        }

        $title = trim($data['target_search']);
        $return_data=array();
        if('maker'==$type){
            $lists = Maker::where('name', 'like', '%'.$title.'%')->get();
            $return_data = array('type'=>'maker', 'lists'=>$lists);
        }elseif('project'==$type){
            $lists = Project::where('subject', 'like', '%'.$title.'%')->get();
            $return_data = array('type'=>'project', 'lists'=>$lists);
        }elseif('investor'==$type){
            $lists = User::where('username', 'like', '%'.$title.'%')->get();
            $return_data = array('type'=>'investor', 'lists'=>$lists);
        }elseif('video'==$type){
            $lists = ActivityProjectVideo::where('subject', 'like', '%'.$title.'%')->get();
            $return_data = array('type'=>'video', 'lists'=>$lists);
        }

        if(count($return_data['lists'])){
            return AjaxCallbackMessage($return_data, true);
        }else{
            return AjaxCallbackMessage('没有搜索到数据', false);
        }
    }

    /**
     * 执行一个创建
     */
    public function postCreate(TopicRequest $request)
    {
        $data = $request->input();
        unset($data['formType'], $data['_token']);
        Topic::create($data);
        return AjaxCallbackMessage('新增成功', true, url('topic/index'));
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = Topic::where('id', $data['id'])->first();

        return view('topic.edit')->with('list', $list);
    }


    /**
     *执行一个编辑
     */
    public function postEdit(TopicRequest $request)
    {
        $data = $request->input();
        unset($data['formType'], $data['_token']);
        Topic::where('id', $data['id'])->update($data);
        return AjaxCallbackMessage('编辑成功', true, url('topic/index'));
    }


}
